package com.tianma315.admin.staff.controller;


import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;

import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.ResponseUtil;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.oss.service.FileService;
import com.tianma315.admin.staff.VO.StaffFileVO;
import com.tianma315.admin.staff.VO.StaffVO;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.staff.domain.StaffDO;
import com.tianma315.admin.staff.service.StaffService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 员工表
 * </pre>
 * <small> 2019-05-27 15:30:33 | Aron</small>
 */
@Controller
@RequestMapping("/staff/staff")
public class StaffController extends BaseController{
	@Autowired
	private StaffService staffService;

	@Autowired
	private FileService fileService;
	
	@GetMapping()
	@RequiresPermissions("staff:staff:staff")
	String Staff(){
	    return "staff/staff/staff";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("staff:staff:staff")
	public Result<Page<StaffVO>> list(Integer pageNumber, Integer pageSize, StaffDO staffDTO){
		// 查询列表数据
        Page<StaffVO> page = staffService.getStaffVOPage(pageNumber,pageSize,staffDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("staff:staff:add")
	String add(){
	    return "staff/staff/add";
	}

	@GetMapping("/edit/{staffId}")
	@RequiresPermissions("staff:staff:edit")
	String edit(@PathVariable("staffId") Integer staffId,Model model){
		StaffDO staff = staffService.getStaffVO(staffId);
		model.addAttribute("staff", staff);
	    return "staff/staff/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("staff:staff:add")
	public Result<String> save( StaffFileVO staff){
		try {
			String url =fileService.upload(staff.getFile().getBytes(),staff.getFile().getOriginalFilename());
			staff.setHealthCertificate(url);
			staff.setCreateBy(getUserId());
			staff.setCreateDate(Calendar.getInstance().getTime());
			if(staffService.addStaff(staff)){
				return Result.ok();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("staff:staff:edit")
	public Result<String>  update( StaffFileVO staff){
		String url = "";
		try {
			if(!staff.getFile().isEmpty()){
				url = fileService.upload(staff.getFile().getBytes(),staff.getFile().getOriginalFilename());
			}
			staff.setHealthCertificate(url);
			staffService.updateById(staff);
		} catch (IOException e) {
			e.printStackTrace();
		}


		return Result.ok();
	}

	/**
	 * 离职操作
	 */
	@ResponseBody
	@RequestMapping("/updateStatus")
//	@RequiresPermissions("staff:staff:edit")
	public Result<String>  updateStatus( Integer staffId,Integer status){
		staffService.updateStatus(staffId,status);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("staff:staff:remove")
	public Result<String>  remove( Integer staffId){
		if(staffService.deleteStaff(staffId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("staff:staff:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] staffIds){
		for (int i = 0; i < staffIds.length; i++) {
			staffService.deleteStaff(staffIds[i]);
		}
		return Result.ok();
	}

	/**
	 * 导出模板
	 */
	@GetMapping( "/exportStaff")
	public void exportStaff(HttpServletResponse response){
		String fileName = "员工导入模板.xls";
		try {
			response = ResponseUtil.getResponse(response,fileName);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		HSSFWorkbook wb =staffService.exportStaff();
		try(OutputStream os = response.getOutputStream()) {
			wb.write(os);
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 进入导入excel
	 */
	@GetMapping( "/staffImport")
	public String staffImport(MultipartFile file){
		return "staff/staff/staffImport";
	}

	/**
	 * 导入excel
	 */
	@ResponseBody
	@PostMapping( "/importStaff")
	public Result importStaff(MultipartFile file){
		if (!file.isEmpty()){
			staffService.importStaff(file,getUserId());
		}
		return Result.ok();
	}
	
}
